/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: future_internal.h
 *
 * Purpose: implementation internal functional for future/promise
 *
 * Developer:
 *   wen.gu , 2020-04-10
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_INTERNAL_FUTURE_INTERNAL_H__
#define __ARA_CORE_INTERNAL_FUTURE_INTERNAL_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include <list>
#include <functional>
#include <mutex>
#include <memory>

#include "ara/core/future_error_domain.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{

/** just for internal imeplementation used */
namespace internal_impl
{

#define FUTURE_FUNC_RET_TYPES internal_impl::template_param_types<decltype(func(std::move(*this)))>
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class FutureInternalState
{
public:
    using auto_lock = std::unique_lock<std::mutex>;
    using func_type = std::function<void(void)>;
public:

    void setCommunicationFunc(func_type&& f)
    {
        auto_lock lock(mLock);

        if (mIsCalled)
        {/** if callbacks are called,then just exe current callbacks at this time */
            lock.unlock();
            f(); /** todo refineme, if f is null? */
            lock.lock();
            mIsCalled = false;
        }
        else
        {
            mFunc = f;
        }
    }

    void DoCommunication()
    {
        auto_lock lock(mLock);
        if(mFunc)
        {
            mFunc();
        }
        
        mFunc = nullptr; /** reset function */
        mIsCalled = true;
    }

private:
    func_type mFunc;
    bool mIsCalled = false;
    std::mutex mLock;
};

/** to expand template parameters(only for Result<T, E> and Future<T, E>) */
template<typename R>
struct template_param_types;


template<template<typename T, typename E>class R, typename T2, typename E2>
struct template_param_types <R<T2, E2> >
{
    using type_t = T2;
    using type_e = E2;
};

} /** namespace internal_impl */
} /** namespace core */
} /** namespace ara */

#endif /** !__ARA_CORE_INTERNAL_FUTURE_INTERNAL_H__ */

